package com.qdu.bussiness.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.qdu.bussiness.constant.BussinessConstant;
import com.qdu.bussiness.domain.BusBill;
import com.qdu.bussiness.domain.BusCoach;
import com.qdu.bussiness.domain.BusTicketPurchase;
import com.qdu.bussiness.mapper.BusCoachMapper;
import com.qdu.bussiness.service.IBusBillService;
import com.qdu.bussiness.service.IBusCoachService;
import com.qdu.bussiness.service.IBusTicketPurchaseService;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.utils.SecurityUtils;
import com.qdu.common.utils.id.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdu.bussiness.mapper.BusHireMapper;
import com.qdu.bussiness.domain.BusHire;
import com.qdu.bussiness.service.IBusHireService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 雇佣记录Service业务层处理
 * 
 * @author qdu
 * @date 2022-03-11
 */
@Service
public class BusHireServiceImpl implements IBusHireService 
{
    @Autowired
    private BusHireMapper busHireMapper;

    @Autowired
    private IBusCoachService busCoachService;

    @Autowired
    private IBusBillService busBillService;

    @Autowired
    private IBusTicketPurchaseService busTicketPurchaseService;

    /**
     * 查询雇佣记录
     * 
     * @param hireId 雇佣记录主键
     * @return 雇佣记录
     */
    @Override
    public BusHire selectBusHireByHireId(Long hireId)
    {
        return busHireMapper.selectBusHireByHireId(hireId);
    }

    /**
     * 查询雇佣记录列表
     * 
     * @param busHire 雇佣记录
     * @return 雇佣记录
     */
    @Override
    public List<BusHire> selectBusHireList(BusHire busHire)
    {
        if(!SecurityUtils.getUsername().equals("admin")){
            busHire.setHireUserName(SecurityUtils.getUsername());
        }
        return busHireMapper.selectBusHireList(busHire);
    }

    /**
     * 新增雇佣记录
     * 
     * @param busHire 雇佣记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBusHire(BusHire busHire)
    {
        // 计算开始时间和时间和结束时间的差值
        Date hireStartTime = busHire.getHireStartTime();
        Date hireEndTime = busHire.getHireEndTime();
        if(hireStartTime == null || hireEndTime == null){
            return AjaxResult.error("请选择雇佣起止时间！");
        }
        long l = hireEndTime.getTime() - hireStartTime.getTime();
        if(l < 3600000L){
            return AjaxResult.error("雇佣时间最少为1小时，请重新选择起止时间！");
        }
        BusBill userTodayBillInfo = busBillService.getUserTodayBillInfo(SecurityUtils.getUsername());
        if(userTodayBillInfo == null || userTodayBillInfo.getBillId() == null){
            return AjaxResult.error("当前用户尚未入园！请先购票！");
        }
        // 检查门票时间与租赁时间关系
        BusTicketPurchase todayTicketPurchase = busTicketPurchaseService.getTodayTicketPurchase();
        if(todayTicketPurchase == null){
            return AjaxResult.error("当前用户尚未验票进园！");
        }
        if(busHire.getHireStartTime().compareTo(todayTicketPurchase.getTicketStartTime()) < 0){
            return AjaxResult.error("雇佣开始时间早于购票入园时间！请重新选择时间！");
        }
        if(busHire.getHireEndTime().compareTo(todayTicketPurchase.getTicketEndTime()) > 0){
            return AjaxResult.error("雇佣结束时间晚于门票结束时间！请重新选择时间！");
        }
        // 获取当前教练的个人信息
        BusCoach busCoach = busCoachService.selectBusCoachByCoachId(busHire.getCoachId());
        // 如果教练的状态不是空闲，不允许雇佣
        if(!BussinessConstant.COACH_STATUS_FREE.equals(busCoach.getCoachStatus())){
            return AjaxResult.error("当前教练非空闲状态，无法雇佣！");
        }
        // 修改教练状态
        BusCoach updateDto = new BusCoach();
        updateDto.setCoachId(busHire.getCoachId());
        updateDto.setCoachStatus(BussinessConstant.COACH_STATUS_BUSY);
        int coachRes = busCoachService.updateBusCoach(updateDto);
        if(coachRes < 1){
            throw new RuntimeException("雇佣失败！");
        }
        long total_hours = l / 3600000L;
        // 向上取整
        int ttl_hours = (int)Math.ceil(total_hours);
        // 雇佣时长
        busHire.setHireTime(String.valueOf(total_hours));
        BigDecimal coachUnitPrice = busCoach.getCoachUnitPrice();
        busHire.setHireUserName(SecurityUtils.getUsername());
        busHire.setIsPaid(0);
        busHire.setStatus("new");
        busHire.setBillId(userTodayBillInfo.getBillId());
        busHire.setHireMoney(String.valueOf(coachUnitPrice.multiply(BigDecimal.valueOf(ttl_hours))));
        busHire.setHireId(IdUtil.getId());
        // 新增雇佣记录
        int res = busHireMapper.insertBusHire(busHire);
        if(res < 1){
            throw new RuntimeException("雇佣失败！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改雇佣记录
     * 
     * @param busHire 雇佣记录
     * @return 结果
     */
    @Override
    public int updateBusHire(BusHire busHire)
    {
        return busHireMapper.updateBusHire(busHire);
    }

    /**
     * 批量删除雇佣记录
     * 
     * @param hireIds 需要删除的雇佣记录主键
     * @return 结果
     */
    @Override
    public int deleteBusHireByHireIds(Long[] hireIds)
    {
        return busHireMapper.deleteBusHireByHireIds(hireIds);
    }

    /**
     * 删除雇佣记录信息
     * 
     * @param hireId 雇佣记录主键
     * @return 结果
     */
    @Override
    public int deleteBusHireByHireId(Long hireId)
    {
        return busHireMapper.deleteBusHireByHireId(hireId);
    }
}
